package parser

import (
	"learngo/crawler/engine"
	"regexp"
)

const cityListRe = `<a href="(http://www.zhenai.com/zhenghun/[0-9a-z]+)"[^>]*>([^<]+)</a>`

func ParseCityList(contents []byte, _ string) engine.ParseResult {
	// 提取页面的城市列表(城市列表解析器)
	re := regexp.MustCompile(cityListRe)
	matches := re.FindAllSubmatch(contents, -1) // 子匹配查询
	result := engine.ParseResult{}              // 页面解析结果
	for _, m := range matches {
		//result.Items = append(result.Items, "City " + string(m[2]))  // item存入城市名
		result.Requests = append(result.Requests, engine.Request{
			Url:    string(m[1]),                                 // requests 存入URL和解析器
			Parser: engine.NewFuncParser(ParseCity, "ParseCity"), // 当解析器为定义时，调用nil会出错，所以定义NilParser
		})
	}
	return result
}
